<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="200px">
      <el-form-item label="校区" prop="shopid">
        <el-select v-model="dataForm.shopid" placeholder= "校区">
          <el-option v-for="(item, index) in shopids"
                     :label="item.shopName"
                     :key="item.shopName"
                     :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="人员类型" prop="position">
        <el-select v-model="dataForm.position" placeholder= "人员类型">
          <el-option v-for="(item, index) in positions"
                     :label="item.name"
                     :key="item.name"
                     :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="老师名字" prop="teachername">
      <el-input v-model="dataForm.teachername" placeholder="老师名字"></el-input>
    </el-form-item>
    <el-form-item label="老师电话" prop="teacherphone">
      <el-input v-model="dataForm.teacherphone" placeholder="老师电话"></el-input>
    </el-form-item>
      <el-form-item label="老师登录密码" prop="password">
        <el-input v-model="dataForm.password" placeholder="********"></el-input>
      </el-form-item>
      <el-form-item label="老师照片" prop="image">
        <el-upload
          class="avatar-uploader"
          :show-file-list="false"
          :action="url"
          :before-upload="beforeUploadHandle"
          :on-success="successHandle">
          <img v-if="imageUrl" :src="imageUrl" class="avatar" style="width:200px;height:auto;">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    <upload v-if="uploadVisible" ref="uploadHandle" ></upload>
  </el-dialog>
</template>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

<script>
  import Upload from './business-upload'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          shopid: '',
          teachername: '',
          teacherage: '',
          teachercourse: '',
          teacherphone: '',
          male: '',
          teacherpic: '',
          briefintroduction: '',
          remark: '',
          createtime: '',
          updatetime: '',
          password: '',
          teacherexperience: ''
        },
        stuct: false,
        imageUrl: '',
        uploadVisible: false,
        males: [{'name': '男', 'id': 1}, {'name': '女', 'id': 0}],
        num: 0,
        successNum: 0,
        shopids: '',
        url: this.$http.adornUrl(`/business/teacher/uploadPicture?token=${this.$cookie.get('token')}`),
        dataRule: {
          shopid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          teachername: [
            { required: true, message: '老师名字不能为空', trigger: 'blur' }
          ],
          teacherage: [
            { required: true, message: '老师年龄不能为空', trigger: 'blur' }
          ],
          teachercourse: [
            { required: true, message: '教学课程不能为空', trigger: 'blur' }
          ],
          teacherphone: [
            { required: true, message: '老师电话不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '登录密码不能为空', trigger: 'blur' }
          ],
          teacherpic: [
            { required: true, message: '老师照片不能为空', trigger: 'blur' }
          ]
        },
        positions: [{'name': '店长', 'id': 1}, {'name': '教练', 'id': 2}, {'name': '仓管', 'id': 3}, {'name': '业务', 'id': 4}]
      }
    },
    components: {
      Upload
    },
    mounted () {
      this.getShopInfo()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/teacher/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.shopid = data.teacher.shopId
                this.dataForm.teachername = data.teacher.teacherName
                this.dataForm.teacherphone = data.teacher.teacherPhone
                this.dataForm.teacherpic = data.teacher.teacherPic
                this.imageUrl = data.teacher.teacherPic
              }
            })
          } else {
            this.dataForm = {}
            this.imageUrl = ''
          }
        })
      },
      beforeUploadHandle (file) {
        if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
          this.$message.error('只支持jpg、png、gif格式的图片！')
          return false
        }
        if (file.size / 1024 / 1024 > 2) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
          return false
        }
        this.num++
      },
      // 上传成功
      successHandle (response) {
        this.successNum++
        if (response && response.code === 0) {
          if (this.num === this.successNum) {
            alert('操作成功, 请继续填写其他信息!')
            this.imageUrl = response.url
            this.dataForm.teacherpic = response.url
          }
        } else {
          this.$message.error(response.msg)
        }
      },
      // 获取机构下所有店铺信息
      getShopInfo () {
        this.$http({
          url: this.$http.adornUrl('/business/student/getShopInfo'),
          mothod: 'get',
          params: {}
        }).then(({data}) => {
          this.shopids = data.list
        })
      },
      // 表单提交
      dataFormSubmit: function () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/teacher/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'shopId': this.dataForm.shopid,
                'position': this.dataForm.position,
                'teacherName': this.dataForm.teachername,
                'teacherPhone': this.dataForm.teacherphone,
                'teacherPic': this.dataForm.teacherpic,
                'password': this.dataForm.password
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
